package view;

import icons.DrawingIcon;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Enumeration;

import socket.Client;
import constants.GlobalDebug;
import controller.ShapeController;

/**
 * 
 * @author pfister@lgi2p 07/11/28
 * 
 */
public class SocketDistributedWhiteboard extends Client {

	public SocketDistributedWhiteboard(int id, Socket sock, String name) {
		super(id, sock, name);
	}

	private WhiteboardFrame whiteboardFrame;
	private PaletteController paletteController;
	private ShapeController shapeController;

	protected void execCommand(String cmd, String args) {
		if (cmd.equals("shape"))
		    shapeController.createShape(args);
		else
			System.out.println("unknown command: "+cmd+" args: "+args);
	}
	
	public void writeCommand(String cmd, String args) {
		if (cmd.equals("shape"))
		    socketWriter.println(D_CREATE_SHAPE + args + EOM);
		else
			System.out.println("unknown command: "+cmd+" args: "+args);
	}

	public static void main(String[] args_) {
		try {
			Socket sock = new Socket("localhost", SERVERPORT);
			SocketDistributedWhiteboard c = new SocketDistributedWhiteboard(-1, sock,
					utils.StringUtils.randomString(5));
			c.run();
			if (c.ui != null && c.exit)
				System.exit(0);
		} catch (UnknownHostException e) {
			System.out.println(ERROR + " 1 " + e.toString());
		} catch (IOException e) {
			System.out.println(ERROR + " 2 " + e.toString());
		} catch (InterruptedException e) {
			System.out.println(ERROR + " 3 " + e.toString());
		} catch (Exception e) {
			System.out.println(ERROR + " 4 " + e.toString());
		}
	}

	/*
	 * Sets the initial display state of the Whiteboard palette. The initially
	 * selected drawing icon is set pressed and others are popped up
	 */
	private void setInitialPaletteState() {
		DrawingIcon diInitiallySelected = paletteController
				.getInitiallySelectedDrawingIcon();
		Enumeration e = whiteboardFrame.getPalette().getAllDrawingIcons();
		DrawingIcon dI;
		while (e.hasMoreElements()) {
			dI = (DrawingIcon) e.nextElement();
			if (dI == diInitiallySelected)
				dI.setState(true);
			else
				dI.setState(false);
		}
	}

	protected void  setupUI() {
		GlobalDebug.isOn = false;
		GlobalDebug.debugCommands = true;
		whiteboardFrame = new WhiteboardFrame("MyWhiteboard");
		StandaloneWhiteboard wb = new StandaloneWhiteboard(whiteboardFrame);
		whiteboardFrame.setSize(640, 480);
		whiteboardFrame.setLocation(0, 0);
		whiteboardFrame.setVisible(true);
		whiteboardFrame.pack();
		paletteController = new PaletteController(whiteboardFrame
				.getPalette());
		setInitialPaletteState();

		shapeController = new ShapeController(whiteboardFrame,
				whiteboardFrame.getShapeContainer(), paletteController);
		shapeController.setSocketClient(this);

		whiteboardFrame.show();

		ui = whiteboardFrame;
		ui.setClient(this);
		whiteboardFrame.setVisible(true);
		whiteboardFrame.setTitle("socket based Whiteboard " + name);
		whiteboardFrame.validate();
	}





}
